﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

using SimbaRGVMonitor.Common.Data;

namespace SimbaRGVMonitor.Common.Utils
{
    public static class SerializeUtil
    {
        private static readonly Lazy<JavaScriptSerializer> jsonSerializer = new Lazy<JavaScriptSerializer>(() => new JavaScriptSerializer());

        public static string SerializeToJson(this object obj)
        {
            return jsonSerializer.Value.Serialize(obj);
        }

        public static T DeserializeFromJson<T>(this string jsonText)
            where T : class
        {
            if(typeof(T) != typeof(JsonNodes))
            {
                return jsonSerializer.Value.Deserialize<T>(jsonText);
            }
            else
            {
                var nodes = jsonSerializer.Value.Deserialize<Dictionary<string, object>>(jsonText);
                var josnNodes = new JsonNodes(nodes);
                return josnNodes as T;
            }
        }
    }
}
